package scenarios.gui.actions;

import scenarios.net.TestNode;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: gvennet
 * Date: 21.02.2010
 * Time: 13:10:24
 */
public class BindingNodes extends AbstractAction
{
	private ArrayList<TestNode> nodes;
	ScheduledThreadPoolExecutor scheduledExecutor;

	public BindingNodes(ArrayList<TestNode> nodes, ScheduledThreadPoolExecutor scheduledExecutor)
	{
		super("Связать узлы");
		this.nodes = nodes;
		this.scheduledExecutor = scheduledExecutor;
	}

	public void actionPerformed(ActionEvent e)
	{
		for (TestNode node : nodes)
		{
			node.startCleaning();
		}

		scheduledExecutor.scheduleAtFixedRate(new Mix(), 0, 1, TimeUnit.MILLISECONDS);
		System.out.println("bind!");
	}

	private class Mix implements Runnable
	{

		@Override
		public void run()
		{
			Random r = new Random();
			nodes.get(r.nextInt(nodes.size())).addNode(nodes.get(0).getInetAddress(), nodes.get(r.nextInt(nodes.size())).getPort());
		}
	}
}